//
//  JQFYTicketSessionTableViewCell.m
//  JQFY
//
//  Created by apple on 2017/9/11.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYTicketSessionTableViewCell.h"

static NSInteger JQFBaseButtonTag = 234;

@interface JQFYTicketSessionTableViewCell ()

@property (nonatomic ,strong) UIButton *creatButton;
@property (nonatomic ,strong) UIButton *selectedButton;

@end

@implementation JQFYTicketSessionTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setupUI];
    }
    return self;
}

-(void) setupUI{
    
    UIView *buttonsView = [[UIView alloc] init];
    [self.contentView addSubview:buttonsView];
    [buttonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).mas_offset(11);
        make.right.mas_equalTo(self.contentView).mas_offset(-11);
    }];
    CGFloat maxContainW = DH_Screen_Width - 11 * 2;
    NSArray *buttons = @[@"2017-9-9 周一 11:30-21:30",@"2017-9-10 周二 11:30-21:30",@"2017-9-1 周三 11:30-21:30",@"两日票",@"三日票"];
    __block CGFloat offsetX = 0;
    __block CGFloat offsetY = 0;
    CGFloat margin = 3;
    [buttons enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:obj forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
        [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"dateLabel"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"dateLabelSelected"] forState:UIControlStateSelected];
        button.layer.cornerRadius = 3;
        button.layer.masksToBounds = YES;
        button.tag = JQFBaseButtonTag + idx;
        [buttonsView addSubview:button];
        CGFloat objW = [obj boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]} context:nil].size.width + margin * 2;
        if (offsetX + objW > maxContainW) { //换行
            offsetX = 0;
            offsetY += (22 + 11);
        }
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(objW);
            make.height.mas_equalTo(22);
            make.leading.equalTo(buttonsView).offset(offsetX);
            make.top.equalTo(buttonsView).offset(offsetY);
        }];
        offsetX += (objW + 11);
    }];
}

+ (CGFloat)heightForCellWithButtons:(NSArray *)buttons {
    CGFloat maxContainW = DH_Screen_Width - 11 * 2;
    __block CGFloat offsetX = 0;
    __block CGFloat offsetY = 0;
    CGFloat margin = 3;
    [buttons enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat objW = [obj boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]} context:nil].size.width + margin * 2;
        
        if (offsetX + objW > maxContainW) { //换行
            offsetX = 0;
            offsetY += (22 + 11);
        }
        offsetX += (objW + 11);
    }];
    offsetY += 22;
    return offsetY;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
